package com.bdqn.tour.servlet.Gyh;

import com.bdqn.tour.pojo.User;
import com.bdqn.tour.service.GyhLoginservice;
import com.bdqn.tour.service.impl.GyhLoginserviceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

/**
 * 登录业务
 */
@WebServlet("/gyhlogin")
public class GyhLoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置字符编码
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        //获取参数
        String phone = req.getParameter("phone");
        String password = req.getParameter("password");
        //调用业务层
        GyhLoginservice service = new GyhLoginserviceImpl();
        //调用业务层登录getLogin方法
        Map<String, Object> resultMap = service.getLogin(phone, password);
        //获取字符输出流
        PrintWriter out = resp.getWriter();
        //存储resultMap数据到session中
        req.getSession().setAttribute("resultMap",resultMap);
        //判断code  0000 登录成功
        String code = resultMap.get("code").toString();
        if("0000".equals(code)){
            //获取user对象
            User user = (User)resultMap.get("user");
            //存储user对象到session中
            req.getSession().setAttribute("user",user);
            //输出成功结果到jsp页面
            out.print("success");
        }else{
            //输出失败原因到jsp页面
              out.println(resultMap.get("msg"));
        }
        out.close();

    }
}

